using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameStateManagement;
using Microsoft.Xna.Framework;
using Microsoft.Phone.Shell;
using System.IO.IsolatedStorage;
using System.Xml.Serialization;
using System.IO;

namespace PiratesOfBermuda
{
    class PauseScreen : MenuScreen
    {
        
        BackgroundScreen backgroundScreen;
        GameScreen gameplayerScreen;

        bool isSinglePlayerMode = true;

        public PauseScreen(GameScreen gameplayerScreen, BackgroundScreen backgroundScreen, bool isSinglePlayer)
            : base(String.Empty)
        {
            IsPopup = true;
            this.backgroundScreen = backgroundScreen;
            this.isSinglePlayerMode = isSinglePlayer;
            this.gameplayerScreen = gameplayerScreen;

            MenuEntry resumeMenuEntry = new MenuEntry("Resume");
            MenuEntry exitMenuEntry;

            if (isSinglePlayerMode)
            {
                exitMenuEntry = new MenuEntry("Save & Quit");
            }
            else
            {
                exitMenuEntry = new MenuEntry("Quit");
            }

            resumeMenuEntry.Selected += ResumeMenuEntrySelected;
            exitMenuEntry.Selected += QuitMenuEntrySelected;

            MenuEntries.Add(resumeMenuEntry);
            MenuEntries.Add(exitMenuEntry);
        }

        protected override void UpdateMenuEntryLocations()
        {
            base.UpdateMenuEntryLocations();

            foreach (var entry in MenuEntries)
            {
                var position = entry.Position;

                position.Y += 60;

                entry.Position = position;
            }
        }

        void ResumeMenuEntrySelected(object sender, EventArgs e)
        {
            backgroundScreen.ExitScreen();
            ExitScreen();
            
        }

        void QuitMenuEntrySelected(object sender, EventArgs e)
        {
            if (!isSinglePlayerMode)
            {
                ScreenManager.AddScreen(new MainMenuScreen());
                backgroundScreen.ExitScreen();
                ExitScreen();
                gameplayerScreen.ExitScreen();
            }
            else
            {
                SaveActiveGameState();
                ScreenManager.AddScreen(new MainMenuScreen());
                backgroundScreen.ExitScreen();
                ExitScreen();
                gameplayerScreen.ExitScreen();
            }
        }

        // Phuong thuc thu ly su kien save game
        private void SaveActiveGameState()
        {
            var res = from screen in ScreenManager.GetScreens()
                      where screen.GetType() == typeof(SinglePlayerScreen)
                      select screen;

            SinglePlayerScreen gameScreen = res.FirstOrDefault() as SinglePlayerScreen;

            if (gameScreen != null)
                SaveToIsolatedStorageFile(gameScreen, MainMenuScreen.fileName);
        }

        /// <summary>
        /// Luu trang thai cua cac doi tuong trong game xuong file
        /// </summary>
        /// <param name="gameScreen">Doi tuong quan ly man hinh choi</param>
        /// <param name="fileName">ten file</param>
        private void SaveToIsolatedStorageFile(SinglePlayerScreen gameScreen, string fileName)
        {
            using (IsolatedStorageFile isolatedStorageFile
                = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream fileStream
                    = isolatedStorageFile.CreateFile(fileName))
                {
                    using (StreamWriter streamWriter = new StreamWriter(fileStream))
                    {
                        // Luu thong tin cua nguoi choi
                        streamWriter.WriteLine(String.Format("Player,{0},{1},{2}",
                                                gameScreen.GameProcessor.player.X, gameScreen.GameProcessor.player.Y,
                                                gameScreen.GameProcessor.playerLifeRemained));
                        // Luu thong tin trang thai cua cac tau dich
                        foreach (BasicEnemy enemy in gameScreen.GameProcessor.enemies)
                        {
                            streamWriter.WriteLine(String.Format("Enemy,{0},{1}", enemy.X, enemy.Y));
                        }
                        // Luu thong tin cua cac dao
                        foreach (Island island in gameScreen.GameProcessor.islands)
                        {
                            streamWriter.WriteLine(String.Format("Island,{0},{1}", island.X, island.Y));
                        }
                        // Luu thong tin cua vung nuoc xoay
                        foreach (Whirl whirl in gameScreen.GameProcessor.whirls)
                        {
                            streamWriter.WriteLine(String.Format("Whirl,{0},{1}", whirl.X, whirl.Y));
                        }
                        // Luu thong tin ve cac tau dich bi no
                        foreach (Rubble rub in gameScreen.GameProcessor.rubbles)
                        {
                            streamWriter.WriteLine(String.Format("Rubble,{0},{1}", rub.X, rub.Y));
                        }
                        streamWriter.Close();
                    }
                }
            }

        }
    }
}
